using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Keep.Model;
using Serilog;
using Keep.Service;
using Keep.Const;
using Microsoft.AspNetCore.Http;
using Keep.Model.Page;

namespace Keep.Controllers;

[ApiController]
[Route("api/[controller]/[action]")]
public class ImgController : ControllerBase
{

    //博客请求参数类
    public class ImgRequest
    {
        public string? path { get; set; }
        public string? oldname { get; set; }
        public string? newname { get; set; }
        public string? name { get; set; }
        public string? keypath { get; set; }
    }

    private readonly IFileService fileService;

    public ImgController(IFileService fileService)
    {
        this.fileService = fileService;
    }

    [HttpPost(Name = "UploadImg")]
    [RequestSizeLimit(long.MaxValue)]
    public async Task<ActionResult<ResultData>> UploadImg(List<IFormFile> file, string? fileurl)
    {
        try
        {
            if (file.Count > 0)
            {
                if (String.IsNullOrEmpty(fileurl))
                {
                    fileurl = Request.Form["fileurl"];
                }
                Log.Warning("UploadImg => " + fileurl);
                fileService.UploadFile(file, fileurl, Static.ImgPath);
                return Ok(new ResultData(Static.Enable, file[0].FileName));
                // return Ok(new { file.Count, Size = fileService.SizeConverter(file.Sum(f => f.Length)) });
            }
            else
            {
                return BadRequest("没有发现上传文件");
            }
        }
        catch (Exception ex)
        {
            return BadRequest(ex.Message);
        }

    }

    [HttpPost(Name = "DelImg")]
    public async Task<ActionResult<ResultData>> DelImg(ImgRequest imgRequest)
    {
        Log.Warning("IN =>   path=" + imgRequest.path + "  name=" + imgRequest.name);
        bool sys = await Task.Run(() => fileService.DelFile(imgRequest.path, Static.ImgPath, imgRequest.name));
        Log.Warning("OUT => " + sys.ToString());
        return Ok(new ResultData(sys ? Static.Enable : Static.Disable, sys));
    }
    [HttpGet(Name = "GetImgPathInfo")]
    public async Task<ActionResult<ResultData>> GetImgPathInfo(string? path, int currentPage, int pageSize)
    {
        Log.Warning("IN => " + path);
        PageResult<FileDiy> sys = await Task.Run(() => fileService.GetPathInfo(Static.ImgPath, path, currentPage, pageSize));
        Log.Warning("OUT => " + sys.ToString());
        return Ok(new ResultData(Static.Enable, sys));
    }
    [HttpPost(Name = "AddImgDirectory")]
    public async Task<ActionResult<ResultData>> AddImgDirectory(ImgRequest imgRequest)
    {
        Log.Warning("IN => " + imgRequest.path);
        string sys = await Task.Run(() => fileService.AddFileDirectory(imgRequest.path, Static.ImgPath));
        Log.Warning("OUT => " + sys.ToString());
        return Ok(new ResultData(!String.IsNullOrEmpty(sys) ? Static.Enable : Static.Disable, sys));
    }
    [HttpPost(Name = "DelImgDir")]
    public async Task<ActionResult<ResultData>> DelImgDir(ImgRequest imgRequest)
    {
        Log.Warning("IN =>   path=" + imgRequest.path + "  name=" + imgRequest.name);
        bool sys = await Task.Run(() => fileService.DelFileDir(imgRequest.path, Static.ImgPath, imgRequest.name));
        Log.Warning("OUT => " + sys.ToString());
        return Ok(new ResultData(sys ? Static.Enable : Static.Disable, sys));
    }
    [HttpPost(Name = "EditImgDirOrName")]
    public async Task<ActionResult<ResultData>> EditImgDirOrName(ImgRequest imgRequest)
    {
        Log.Warning("IN => path=" + imgRequest.path + "    oldname=" + imgRequest.oldname + "    newname=" + imgRequest.newname);
        bool sys = await Task.Run(() => fileService.EditFileDirOrName(imgRequest.path, Static.ImgPath, imgRequest.oldname, imgRequest.newname));
        Log.Warning("OUT => " + sys.ToString());
        return Ok(new ResultData(sys ? Static.Enable : Static.Disable, sys));
    }
}
